﻿using Nop.Core.Domain.Stores;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SpotsHub.Tools.MassUploader.Forms
{

    public partial class DatabaseProducts : Form
    {
        private IList<Store> stores;
        public DatabaseProducts()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(textBox1.Text))
            {
                stores = DataAdapter.Instance.GetStoreByName(textBox1.Text);
                if (stores != null)
                {
                    dataGridView1.DataSource = stores;

                    var sis = stores.Select(s => new StoreInfo() { Id = s.Id, Name = s.Name }).ToList();
                    comboBox1.DataSource = sis;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (stores != null && comboBox1.SelectedItem != null)
            {
                var s = comboBox1.SelectedItem as StoreInfo;
                var products = DataAdapter.Instance.GetAllProducts(true, s.Id);
                if (products.Count > 0 && products != null)
                {
                    dataGridView2.DataSource = products;

                    if (dataGridView2.Columns[0].Name != "Attributes")
                    {
                        DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
                        buttonColumn.HeaderText = "";
                        buttonColumn.Name = "Attributes";
                        buttonColumn.Text = "Attributes";
                        buttonColumn.UseColumnTextForButtonValue = true;

                        dataGridView2.Columns.Insert(0, buttonColumn);
                    }
                }
            }
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && dataGridView2.Columns[0].Name == "Attributes")
            {
                var dlg = new ProductAttribute();
                dlg.ProductNameString = dataGridView2.Rows[e.RowIndex].Cells["Name"].Value as string;
                dlg.ProductAttributes = DataAdapter.Instance.GetProductAttributeNames();
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (dlg.NewProductAttributes.Count > 0)
                    {
                        foreach (var n in dlg.NewProductAttributes.Keys)
                        {
                            DataAdapter.Instance.AddProductAttribute(n, dlg.NewProductAttributes[n]);
                        }
                    }

                    var values = new List<Nop.Core.Domain.Catalog.ProductVariantAttributeValue>();

                    foreach (DataRow r in dlg.ProductAttributeValues.Rows)
                    {
                        values.Add(new Nop.Core.Domain.Catalog.ProductVariantAttributeValue()
                        {
                            IsPreSelected = (bool)r["IsPreSelected"],
                            Name = (string)r["ValueName"],
                            DisplayOrder = r["Order"] is DBNull ? 1 : (int)r["Order"],
                            PriceAdjustment = r["PriceAdjustment"] is DBNull ? 0M : (decimal)r["PriceAdjustment"],
                            WeightAdjustment = r["WeightAdjustment"] is DBNull ? 0M : (decimal)r["WeightAdjustment"]
                        });
                    }
                    var rr = dataGridView2.Rows[e.RowIndex];
                    int productId = (int)rr.Cells["Id"].Value;
                    DataAdapter.Instance.AddProductAttributeValue(
                        productId,
                        dlg.SelectedProductAttribute,
                        dlg.SelectedControlType,
                        dlg.IsRequired,
                        values);
                }
            }
        }
    }

    public class StoreInfo
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return String.Format("{0}: \t{1}", Id, Name);
        }
    }

}
